﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;

namespace HCFramework.Product.Admin.OrderManage
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFilterByStatus();
            }
        }

        private void BindFilterByStatus()
        {
            foreach (int value in Enum.GetValues(typeof(E.OrderStatus)))
            {
                string text = C.OrderController.GetStatusName(value);
                radioListFilterByStatus.Items.Add(new ListItem(text, value + ""));
            }
            radioListFilterByStatus.Items.Add(new ListItem("Tất cả", ""));
            radioListFilterByStatus.SelectedValue = (int)E.OrderStatus.Waiting + "";
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Orders
                         select a;

            if (radioListFilterByStatus.SelectedValue != "")
            {
                result = from r in result
                         where r.Status == int.Parse(radioListFilterByStatus.SelectedValue)
                         select r;
            }

            result = result.OrderByDescending(ik => ik.CreatedDate);

            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //if (e.CommandName == "Xoa")
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    RoleController.Delete(Guid.Parse(item["RoleID"].Text));
            //}
        }

        protected void RadGridMain_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int orderId = int.Parse((e.Item as GridDataItem)["OrderID"].Text);
                Order order = C.OrderController.GetByID(orderId);

                Literal litCreatedDate = e.Item.FindControl("litCreatedDate") as Literal;
                Literal litProducts = e.Item.FindControl("litProducts") as Literal;
                Literal litTotalPrice = e.Item.FindControl("litTotalPrice") as Literal;
                Literal litDeliveryMethod = e.Item.FindControl("litDeliveryMethod") as Literal;
                Literal litPaymentMethod = e.Item.FindControl("litPaymentMethod") as Literal;
                HyperLink linkDetail = e.Item.FindControl("linkDetail") as HyperLink;
                HyperLink linkSendEmail = e.Item.FindControl("linkSendEmail") as HyperLink;
                DropDownList cboStatus = e.Item.FindControl("cboStatus") as DropDownList;

                litCreatedDate.Text = order.CreatedDate.ToString("dd/MM/yyyy, hh:mm");
                litProducts.Text = GetDisplayProducts(order);
                litTotalPrice.Text = C.OrderController.GetTotalPrice(orderId).ToString("N0");
                litDeliveryMethod.Text = E.Method.GetDisplayName_DeliveryType((E.DeliveryType)order.DeliveryType);
                litPaymentMethod.Text = E.Method.GetDisplayName_PaymentMethod((E.PaymentMethod)order.PaymentMethod);
                //litStatus.Text = E.Method.GetDisplayName_OrderStatus((E.OrderStatus)order.Status);
                linkDetail.NavigateUrl += order.Identity + "";
                linkSendEmail.NavigateUrl += orderId + "";
                foreach (int value in Enum.GetValues(typeof(E.OrderStatus)))
                {
                    string text = E.Method.GetDisplayName_OrderStatus((E.OrderStatus)value);
                    cboStatus.Items.Add(new ListItem(text, value + ""));
                }
                cboStatus.SelectedValue = order.Status + "";
                cboStatus.Attributes["OrderID"] = orderId + "";
            }
            if (e.Item.ItemType == GridItemType.Footer)
            {
                
            }
        }

        private string GetDisplayProducts(Order order)
        {
            string ret = "";
            string template = M.FileM.Read(@"\Product\Admin\OrderManage\Template\ProductItem.htm");
            foreach (Order_Product op in order.Order_Products)
            {
                ret += ", " + M.Template.MergeOrder_Product(template, op);
            }
            ret = ret.TrimStart(',').TrimStart();
            return ret;
        }

        protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList cboStatus = sender as DropDownList;
            int orderId = int.Parse(cboStatus.Attributes["OrderID"]);
            HCDataContext db = new HCDataContext();
            Order order = C.OrderController.GetByID(orderId, db);
            order.Status = int.Parse(cboStatus.SelectedValue);
            C.OrderController.Update(order, db);
        }
    }
}